var map = null; //地图对象
var circle = null;

var kongxian=0;//空闲
var manglu=0;//忙碌
var lixian=0;//未上班


var ctx=window.daijia.ctx;
var longitude=window.daijia.lng;
var latitude=window.daijia.lat;
var orderId=window.daijia.orderId;
var orderNumber=window.daijia.orderNo;

var point = new BMap.Point(longitude,latitude);

var memberArr=[];//司机标记集合

function paigeita(driverId){
	if(window.confirm('确认要派单给该服务人员吗？')){
		$.post(ctx+'/errand/paidan',{'driverId':driverId,'orderId':orderId},function(data){
			if(data.success){
		    		
	    		$.scojs_message('派单成功', $.scojs_message.TYPE_OK);
				$(".tcb-btn").hide();//隐藏派单按钮
				$(".tcb-btn").parent().parent().hide();

	    		window.location.href=ctx+"/errand/up/list";
			}
			else{
				$.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
			}
		});
	}
}

function close_driver_window(thiswindow){
	$(thiswindow).parent().parent().fadeOut(200);
}
    
function initialize(){

	map = new BMap.Map("allmap");  
	map.centerAndZoom(point, 14);
    map.enableScrollWheelZoom();
    
    var marker = new BMap.Marker(point);  // 创建标注
	map.addOverlay(marker);
	
	circle = new BMap.Circle(point,2000);//默认2公里
	map.addOverlay(circle);
    
    getDrivers();
}

function getDrivers(){
	$.get(ctx+'/errand/listpaidan',{'orderId':orderId},function(data){
		if(data && data.length>0){
			$(data).each(function(i,driver){
				
				var status=driver.status;
				
				var showStatus=0;
				var detailStatus='lixian';
				
				if(status==0){
					showStatus=0;
					detailStatus='lixian';
					++lixian;
				}
				else if(status==1){
					showStatus=1;
					detailStatus='kongxian';
					++kongxian;
				}
				else{
					showStatus=2;
					detailStatus='manglu';
					++manglu;
				}
				
				var htm='';
				
				if(status==0){
					htm='<div id="driver_marker_'+driver.id+'" class="driver_marker_class" style="position:absolute;z-index:999;width:55px;display:none;">'+
							'<div class="em-lixian">'+driver.realname+'('+driver.paotuiAmount+')</div>'+
					     '</div>';
				}
				else if(status==1){
					htm='<div id="driver_marker_'+driver.id+'" class="driver_marker_class" style="position:absolute;z-index:999;width:55px;">'+
							'<div class="em-kongxian">'+driver.realname+'('+driver.paotuiAmount+')</div>'+
					    '</div>';
				}
				else{
					htm='<div id="driver_marker_'+driver.id+'" class="driver_marker_class" style="position:absolute;z-index:999;width:55px;display:none;">'+
						'<div class="em-manglu">'+driver.realname+'('+driver.paotuiAmount+')</div>'+
				    '</div>';
				}
				
					
			    var driverpoint = new BMap.Point(driver.longitude,driver.latitude);
				var drivermarker = new BMapLib.RichMarker(htm, driverpoint, {"anchor": new BMap.Size(-39, -40), "enableDragging": true});
				drivermarker.driverId=driver.id;
				drivermarker.driverstatus=showStatus;
				
				map.addOverlay(drivermarker);
			
			
				var photo=ctx+'/assets/img/default_avatar.jpg';
				if(driver.photo && driver.photo!=''){
					photo=ctx+'/upload/driver/'+driver.photo;
			    }
				var companyName_All=driver.company.companyName;
				if(companyName_All.length>12){
					companyName_All=companyName_All.substr(0,12)+"...";
				}
				var content='<div id="driver_detail_'+driver.id+'" class="em-tc-box" style="position:absolute;z-index:9999;width:500px;display:none;">'+
						    	'<div class="tcb-con">'+
									'<div class="tcb-close" onclick="close_driver_window(this);"><img src="'+ctx+'/assets/css/images/tcb_close_bg_03.png"></div>'+
									'<dl>'+
								    	'<dt><img src="'+photo+'" width="96" height="96"></dt>'+
								        '<dd>'+
								            '<p>公司：'+companyName_All+'</p>'+
								            '<p>姓名：'+driver.realname+'('+driver.username+')</p>'+
								            '<p>电话：'+driver.phone+'</p>'+
								            '<p>当前跑腿单：</p>'+
								            '<p>&nbsp;</p>'+
								        '</dd>'+
								    '</dl>'+
								    '<div class="tcb-btn" style="display:none;"><a href="javascript:;" id="paigeita_'+driver.id+'" onclick="paigeita('+driver.id+');">将工单'+orderNumber+'派给他/她</a></div>'+
								'</div>'+
							'</div>';
				
				var infowindow = new BMapLib.RichMarker(content, driverpoint, {"anchor": new BMap.Size(-210, -330), "enableDragging": true});
				//司机详细
				map.addOverlay(infowindow);
				
				drivermarker.addEventListener("onmouseup", function(e){
					if(e.which==3){
						$(".em-tc-box").fadeOut(0);//隐藏所有面板
						
						infowindow.setPosition(new BMap.Point(map.getCenter().lng,map.getCenter().lat));
						$("#driver_detail_"+driver.id+"").fadeIn(200);//显示当前面板
						
						$(".tcb-btn").hide();//隐藏所有派单按钮
						
						//如果选择了订单 并且司机为空闲 显示派单按钮
						if(status!=0){
							$("#paigeita_"+driver.id+"").parent().show();
						}
					}
				}); 
				
				memberArr.push(drivermarker);
				
			});
		}
		updateDriverStatusNum();
		
	});
	
}

function getMarkerByDriverId(driverId,driverstatus){
	if(memberArr==null || memberArr.length==0){
		return null;
	}
	var dm=null;
	$(memberArr).each(function(index,driverMarker){
		if(driverMarker.driverId == driverId){
			if(driverstatus){
				driverMarker.driverstatus=driverstatus;
			}
			dm=driverMarker;
			return false;
		}
	});
	return dm;
}


function updateDriverStatusNum(){
	
	$("#kongxianDrivers").html(kongxian);
	$("#mangluDrivers").html(manglu);
	$("#lixianDrivers").html(lixian);
	
	$("#allDrivers").html(lixian+kongxian+manglu);
	
}

$(document).ready(function(){
	
	initialize();
	
	$("#scope").change(function(){
		circle.remove();
		circle = new BMap.Circle(point,$(this).val());
		map.addOverlay(circle);
	});
	
	$("#refresh-driver").click(function(){
		map.clearOverlays();
		map.addOverlay(new BMap.Marker(point));
		circle = new BMap.Circle(point,$("#scope").val());
		map.addOverlay(circle);
		kongxian=0;
		manglu=0;
		lixian=0;
		memberArr=[];
		getDrivers();
	});
	
	
	var isFullscreen = false;
	var panelHeight = $("#realMap").height();
    var panelWidth = $("#realMap").width();
    var maxWidth = screen.width;
	var maxHeight = document.documentElement.clientHeight-60;
    $("#fullscreen").click(function () {
        if (!isFullscreen) {
            $("#realMap").css("position", "absolute").css("background-color", "#f5f5f5").css("top", 0).css("left", 0).css("width", maxWidth).css("height", maxHeight).css("z-index", "99999");
            $("#allmap").css("height",maxHeight);
            isFullscreen = true;
            $(this).text("退出全屏");
        } else {
            $("#realMap").css("position", "relative").css("width", panelWidth).css("height", panelHeight);
            $("#allmap").css("height",560);
            isFullscreen = false;
            $(this).text("全屏显示");
        }
    });
    
    $("#showAll").click(function(){
    	initializeColor();
    	$("#showAll").removeClass("allstatus").addClass("driver-status allstatus_active");
    	showDriversByStatus(null);
    });
    $("#showKongxian").click(function(){
    	initializeColor();
    	$("#showKongxian").removeClass("kongxian").addClass("driver-status kongxian_active");
    	showDriversByStatus(1);
    });
    $("#showManglu").click(function(){
    	initializeColor();
    	$("#showManglu").removeClass("manglu").addClass("driver-status manglu_active");
    	showDriversByStatus(2);
    });
    $("#showLixian").click(function(){
    	initializeColor();
    	$("#showLixian").removeClass("lixian").addClass("driver-status lixian_active");
    	showDriversByStatus(0);
    });

    
});	

function showDriversByStatus(status){
	if(memberArr==null || memberArr.length==0){
		return null;
	}
	if(null!=status){
		$(".driver_marker_class").hide();
		$(memberArr).each(function(index,driverMarker){
			if(null!=driverMarker.driverId && null!=driverMarker.driverstatus && driverMarker.driverstatus==status){
				$("#driver_marker_"+driverMarker.driverId+"").fadeIn(200);
			}
		});
	}
	else{
		$(".driver_marker_class").fadeIn(200);
	}
}

function initializeColor(){
	$("#showAll").removeClass("allstatus_active driver-status").addClass("allstatus");
	$("#showKongxian").removeClass("kongxian_active driver-status").addClass("kongxian");
	$("#showManglu").removeClass("manglu_active driver-status").addClass("manglu");
	$("#showLixian").removeClass("lixian_active driver-status").addClass("lixian");
}